//
//  XGL_GuideView.swift
//  ProductNumberOne
//
//  Created by ios on 2024/4/22.
//

import UIKit
import MXCommonModule

class XGL_GuideView: UIView {

    typealias StartAction = ()->Void
    var scrollView: UIScrollView!
    var pageControl = UIPageControl()
    private var startButton: UIButton!
    private var handler: StartAction?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func showGuide(complete: StartAction?){
        self.handler = complete
       
        self.scrollView = UIScrollView().then({ make in
            make.showsHorizontalScrollIndicator = false
            make.showsVerticalScrollIndicator = false
            make.delegate = self
            make.isPagingEnabled = true
            make.bounces = true
        })
        let contentView = UIView()
        self.addSubview(scrollView)
        scrollView.addSubview(contentView)
        scrollView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        contentView.snp.makeConstraints { (make) in
            make.edges.equalTo(scrollView)
            make.height.equalTo(scrollView)
        }
        self.pageControl = UIPageControl().then({ make in
            make.backgroundColor = UIColor.clear
            make.numberOfPages = 2
            make.currentPage = 0
            make.pageIndicatorTintColor = UIColor.gray
            make.currentPageIndicatorTintColor = UIColor.hexColor("#4C69FF")
        })
        
        let guideImages = [UIImageView(image: imageWithName("img_xgl_guide_1")),UIImageView(image: imageWithName("img_xgl_guide_2")),UIImageView(image: imageWithName("img_xgl_guide_3"))]
        var leftItem: UIImageView?
        for i in 0..<guideImages.count {
            let guideImageView = guideImages[i]
            guideImageView.isUserInteractionEnabled = true
            let nextButton = UIButton().then { make in
                make.tag = i + 1000
                if i == guideImages.count - 1{
                    make.addTarget(self, action: #selector(self.startAction), for: .touchUpInside)
                }else{
                    make.addTarget(self, action: #selector(self.nextAction(sender:)), for: .touchUpInside)
                }
            }
            guideImageView.addSubview(nextButton)
            nextButton.snp.makeConstraints { make in
                make.centerX.equalToSuperview()
                make.width.equalToSuperview().multipliedBy(0.45)
                make.height.equalTo(conversionWithScrennWidth(value: 80.0))
                make.bottom.equalTo(-24 - BottomHeight)
            }
            contentView.addSubview(guideImageView)
            guideImageView.snp.makeConstraints { make in
                make.width.equalTo(ScreenWidth)
                make.centerY.height.equalToSuperview()
                if let item = leftItem {
                    make.left.equalTo(item.snp.right)
                }else{
                    make.left.equalToSuperview()
                }
            }
            leftItem = guideImageView
        }
        contentView.snp.makeConstraints { make in
            if let item = leftItem {
                make.right.equalTo(item.snp.right)
            }
        }
    }
}

extension XGL_GuideView{
    @objc private func nextAction(sender: UIButton){
        let index = sender.tag - 1000
        self.pageControl.currentPage = (1 + index)
        self.scrollView.scrollRight()
    }
    @objc private func startAction(){
        if let block = self.handler{
            block()
        }
    }
}

extension XGL_GuideView: UIScrollViewDelegate{
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let offset = scrollView.contentOffset
        
        pageControl.currentPage = NSInteger(offset.x / scrollView.width)
    }
}
